<?php

include_once './log4php/Logger.php';

class Utils {

    public static function toString($value) {
        if ($value === NULL) {
            return "NULL";
        }

        if (is_array($value)) {
            $arrayStr = "[";
            foreach ($value as $key => $value) {
                if (strlen($arrayStr) > 1) {
                    $arrayStr .= ", ";
                }
                $arrayStr .= "'" . $key . "' = '" . Utils::toString($value) . "'";
            }
            $arrayStr .= "]";
            return $arrayStr;
        }

        if (is_object($value) && ($value instanceof Exception)) {
            $str = "EXCEPTION: \nClass: " . get_class($value);
            $str .= ", \nCode: " . $value->getCode();
            $str .= ", \nMessage: " . $value->getMessage();
            $str .= ", \nOccured in " . $value->getFile() . " at line " . $value->getLine();

            if ($value instanceof FacebookApiException) {
                $str .= ", \nType: " . $value->getType();
                $str .= ", \nResult: " . Utils::toString($value->getResult());
            }
            
            $str .= ", \nTrace: " . $value->getTraceAsString();
            return $str;
        }

        if (is_object($value)) {
            $arrayStr = "";

            foreach (get_object_vars($value) as $key => $val) {
                if (strlen($arrayStr) > 0) {
                    $arrayStr .= ", ";
                }
                $arrayStr .= "'" . $key . "' = '" . Utils::toString($val) . "'";
            }
            $arrayStr .= "}";
            return get_class($value) . " instance {" .$arrayStr;
        }

        return $value;
    }

    /**
     * Collects/parses the script input parameters in the following order:
     * 1. in case of POST: if POST data is a valid JSON-encoded associative array
     * 2. the query string's "params" param is its value if valid JSON-encoded associative array
     * 3. the query string's paramseter
     *
     * @return array array of params
     */
    public static function getInputParameters() {
        $result_array = NULL;

        $postdata = file_get_contents("php://input");
        if ($postdata) {
            // json_decode() returns null if the passed $postdata is not a valid JSON-string
            $result_array = json_decode($postdata, TRUE);
        }

        // if the POST data is not a valid JSON-encoded string
        if (!$result_array) {
            $queryStringParams = array_merge($_GET, $_POST);

            if (isset($queryStringParams["params"])) {
                $result_array = json_decode($queryStringParams["params"], TRUE);
            }

            // if the "params" query parameter not found, or is not valid JSON-string
            if (!$result_array) {
                $result_array = $queryStringParams;
            }
        }        

        if (!$result_array) {
            $result_array = array();
        }

        return $result_array;
    }

    /**
     *
     * Returns the value of the given input parameter, or the default value is not found.
     * @param array $inputParamsArray the array of input parameters
     * @param string $paramName the name if the requested input parameter
     * @param string $defaultValue the default value of the parameter
     * @return string the values of the requested parameter, or the default value if not found
     */
    public static function getInputParameterValue($inputParamsArray, $paramName, $defaultValue=NULL) {
        return isset($inputParamsArray[$paramName]) ? $inputParamsArray[$paramName] : $defaultValue;
    }

    /**
     *
     * @param array $inputArray
     * @return <type>
     */
    public static function getLongitude($inputArray) {
        if (!isset($inputArray[PARAM_LON])) {
            throw new Exception(
                    'Missing argument: ' . PARAM_LON,
                    ERROR_UPDATELOCATION_LONLAT_MISSING);
        }

        if (!is_numeric($inputArray[PARAM_LON]) || $inputArray[PARAM_LON] > 180.0 || $inputArray[PARAM_LON] < -180.0) {
            throw new Exception(
                    'Wrong lon value: ' . $inputArray[PARAM_LON] . '. Longitude must be between -180 and 180!',
                    ERROR_UPDATELOCATION_LONLAT_INVALID);
        }

        return $inputArray[PARAM_LON];
    }

    /**
     *
     * @param array $inputArray
     * @return <type>
     * @throws Exception
     */
    public static function getLatitude($inputArray) {
        if (!isset($inputArray[PARAM_LAT])) {
            throw new Exception(
                    'Missing argument: ' . PARAM_LAT,
                    ERROR_UPDATELOCATION_LONLAT_MISSING);
        }

        if (!is_numeric($inputArray[PARAM_LAT]) || $inputArray[PARAM_LAT] > 90.0 || $inputArray[PARAM_LAT] < -90.0) {
            throw new Exception(
                    'Wrong lat value: ' . $inputArray[PARAM_LAT] . '. Latitude must be between -90 and 90!',
                    ERROR_UPDATELOCATION_LONLAT_INVALID);
        }

        return $inputArray[PARAM_LAT];
    }

    /**
     *
     * @param Logger $log
     */
    public static function logPhpGlobals($log) {
        $log->debug('$_REQUEST[] = ' . self::toString($_REQUEST));
        $log->debug('$_POST[] = ' . self::toString($_POST));
        $log->debug('$_GET[] = ' . self::toString($_GET));
        $log->debug('$_COOKIE[] = ' . self::toString($_COOKIE));
        $log->debug('$_SERVER[] = ' . self::toString($_SERVER));
        $log->debug('$_ENV[] = ' . self::toString($_ENV));
    }

}



?>
